package com.digitalchina.lxnsy.viewholder;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.activity.old.SignInInfoActivity;
import com.digitalchina.lxnsy.entity.TrainingVO;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.digitalchina.lxnsy.toolkit.StringUtil;
import com.digitalchina.lxnsy.toolkit.Utils;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.viewholder
 * 创建人：白彦东
 * 创建时间：2016/12/28 18:10
 * 修改备注：
 * ================================================
 */

public class SignInListHolder extends BaseViewHolder<TrainingVO> {

    private TextView tvTrainName;
    private TextView tvTrainExpert;
    private TextView tvSignIn;
    private ImageView ivDisplayStatus;
    private TextView tvTrainTime;
    private TextView tvTrainAddress;
    private TextView tvSignNum;

    private static String IS_SIGNED = "1";
    private static String IS_NO_SIGNED = "0";
    private static String IS_EXPIRED = "0";
    private static String IS_NO_EXPIRED = "1";
    private final int SIGN_REQUEST = 131;

    public SignInListHolder(ViewGroup parent) {
        super(parent, R.layout.item_sign_in_list);
        tvTrainName = $(R.id.item_sign_tv_training_name);
        tvTrainExpert = $(R.id.item_sign_tv_training_person);
        tvSignIn = $(R.id.item_sign_tv_sign_in);
        ivDisplayStatus = $(R.id.item_sign_iv_display_status);
        tvTrainTime = $(R.id.item_sign_tv_training_time);
        tvTrainAddress = $(R.id.item_sign_tv_training_address);
        tvSignNum = $(R.id.item_sign_tv_sign_in_num);
    }


    public void setData(final TrainingVO trainingVO) {
        super.setData(trainingVO);
        tvTrainName.setText(trainingVO.getTitle() != null ? trainingVO.getTitle() : "");
        tvTrainExpert.setText(String.format("培训人: %s", trainingVO.getExpert() != null ? trainingVO.getExpert().getExpertName() != null ? trainingVO.getExpert().getExpertName() : "" : ""));
        //标签显示判断
        if (trainingVO.getIsexpired().equals(IS_EXPIRED)) { // 过期
            ivDisplayStatus.setVisibility(View.VISIBLE);
            ivDisplayStatus.setImageResource(R.drawable.icon_sign_out_date);
            tvSignIn.setVisibility(View.GONE);
        } else if (trainingVO.getIssign().equals(IS_NO_SIGNED)) { // 未签到
            if (!StringUtil.isStrEmpty(trainingVO.getLocationAddress())) { // 定位成功
                if (trainingVO.getTrainaddress().contains(trainingVO.getLocationAddress())) { // 如果培训地址包含定位区域 显示签到按钮
                    ivDisplayStatus.setVisibility(View.GONE);
                    tvSignIn.setVisibility(View.VISIBLE);
                    tvSignIn.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if (Utils.isLogin(getContext()))
                                GotoUtil.gotoForResultActivity((Activity) getContext(), SignInInfoActivity.class, SIGN_REQUEST, "trainingCode", trainingVO.getCode());
                            else
                                Utils.notLoginGoto(getContext());
                        }
                    });
                } else { // 不再定位区域内 显示距离过远
                    ivDisplayStatus.setVisibility(View.VISIBLE);
                    ivDisplayStatus.setImageResource(R.drawable.icon_sign_too_far);
                    tvSignIn.setVisibility(View.GONE);
                }
            } else { // 定位失败 默认显示距离过远
                ivDisplayStatus.setVisibility(View.VISIBLE);
                ivDisplayStatus.setImageResource(R.drawable.icon_sign_too_far);
                tvSignIn.setVisibility(View.GONE);
            }
        } else { // 已签
            ivDisplayStatus.setVisibility(View.VISIBLE);
            tvSignIn.setVisibility(View.GONE);
            ivDisplayStatus.setImageResource(R.drawable.icon_sign_signed);
        }

        if (trainingVO.getTraindate() != null && trainingVO.getStarttime() != null && trainingVO.getEndtime() != null)
            tvTrainTime.setText(String.format("培训时间: %s", StringUtil.jointString(trainingVO.getTraindate(), " ", trainingVO.getStarttime(), " - ", trainingVO.getEndtime())));
        else
            tvTrainTime.setText(String.format("培训时间: %s", ""));
        if (trainingVO.getTrainaddress() != null)
            tvTrainAddress.setText(String.format("培训地点: %s", trainingVO.getTrainaddress()));
        else
            tvTrainAddress.setText(String.format("培训地点: %s", ""));
        if (trainingVO.getAttendanceNum() != null)
            tvSignNum.setText(String.format("%s人", trainingVO.getAttendanceNum()));
        else
            tvSignNum.setText(String.format("%s人", 0));
    }
}
